<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['commodity:goods:productAssessProcess']"
          >审核通过</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          @click="handleFailure"
          v-hasPermi="['commodity:goods:productAssessProcess']"
          >审核失败</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bargainList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50px" align="center">
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="评价内容" align="center" prop="content" />
      <el-table-column label="评价星级" align="center" prop="level" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="" v-if="scope.row.status == 0">待审核</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 1">成功</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status == 2">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="提交人id" align="center" prop="submitId" /> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品议价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="价格名称" prop="bargainName">
          <el-input
            v-model="form.bargainName"
            placeholder="请输入价格名称_加价/减价"
          />
        </el-form-item>
        <el-form-item label="基础售价" prop="basePercent">
          <el-tooltip
            class="item"
            effect="dark"
            content="例:50即为50/100=50%,例如：商品售价为100元，加价后，售价为100+100*50%=150"
            placement="top-start"
          >
            <el-input
              v-model="form.basePercent"
              placeholder="请输入基础售价的百分比"
              type="number"
              onkeyup="value=value.replace(/[^\d]/g,'')"
            >
              <template slot="append"> % </template>
            </el-input>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="操作类别" prop="opType">
          <el-select v-model="form.opType" placeholder="请选择操作类别">
            <el-option label="默认价格" :value="0"> </el-option>
            <el-option label="添加" :value="1"> </el-option>
            <el-option label="减少" :value="2"> </el-option>
            <el-option label="折扣" :value="3"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBargain,
  getBargain,
  delBargain,
  addBargain,
  updateBargain,
} from "@/api/system/bargain";
import { commentList, commentShenhe } from "@/api/product/comment";
export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品议价表格数据
      bargainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 表单参数
      form: {
        mallId: "",
      },
      // 表单校验
      rules: {
        bargainName: [
          { required: true, message: "请输入价格名称", trigger: "blur" },
        ],
        basePercent: [
          { required: true, message: "请输入基础售价", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value < 1 || value > 100) {
                return callback(new Error("请输入1-100之间的整数"));
              } else {
                return callback();
              }
            },
          },
        ],
        opType: [
          { required: true, message: "请选择操作类别", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品议价列表 */
    getList() {
      this.loading = true;
      commentList(this.queryParams).then((response) => {
        this.bargainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bargainName: null,
        opType: null,
        basePercent: null,
        createTime: null,
        updateTime: null,
        mallId: null,
        submitId: null,
        expOne: null,
        expTwo: null,
        expThree: null,
        expFour: null,
        expFive: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      const data = {
        idList: this.ids,
        status: 1,
      };
      commentShenhe(data).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    // 失败
    handleFailure() {
      const data = {
        idList: this.ids,
        status: 2,
      };
      commentShenhe(data).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBargain(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品议价";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBargain(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBargain(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品议价编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBargain(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "commodity/bargain/export",
        {
          ...this.queryParams,
        },
        `bargain_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
